import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { filter, map } from 'rxjs/operators';

interface e {
  key: any;
  value?: any;
}

@Injectable({
  providedIn: 'root'
})
export class BroadcastService {
  private _event: Subject<e>;
  constructor() {
    this._event = new Subject<e>();
  }

  broadcast(key: any, value?: any) {
    this._event.next({ key, value });
  }
  on(key: any) {
    return this._event.asObservable().pipe(
      filter(e => e.key === key),
      map(e => e.value)
    )
  }
}
